/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox.cellRenderer;

import java.awt.Component;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox.cellRenderer
 * @date 19/02/2014 - 21:11:35
 */
public class IconListRenderer extends JLabel implements ListCellRenderer<ImageIcon> {
	
	private static final long serialVersionUID = 7499307854812530891L;
	
	/**
     * 
     */
	public IconListRenderer() {
	
		setOpaque(true);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
	 */
	@Override
	public Component getListCellRendererComponent(final JList<? extends ImageIcon> list, final ImageIcon value, final int index, final boolean isSelected, final boolean cellHasFocus) {
	
		//
		if(isSelected) {
			
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
			
		}else {
			
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}
		
		//
		if(value != null) {
			
			//
			final ImageIcon imageIcon = new ImageIcon(value.getImage().getScaledInstance(30, 25, Image.SCALE_DEFAULT));
			
			imageIcon.setDescription(value.getDescription());
			imageIcon.setImageObserver(value.getImageObserver());
			
			//
			setText(imageIcon.getDescription());
			setIcon(imageIcon);
			setToolTipText(imageIcon.getDescription());
		}
		
		return this;
	}
}
